<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/21
 * Time: 13:21
 */

namespace frontend\models;

use common\models\FeedsModel;
use yii\base\Model;
class FeedsForm extends Model
{

    public $content;

    public $_lastError;

    /**
     * 字段规则
     * @return array
     */
    public  function rules()
    {
        return [
            [['content'],'required'],
            [['content'],'string','max'=>255]
        ];
    }


    public function attributeLabels()
    {
        return [
            'id'=>'ID',
            'content'=>'内容'
        ];
    }

    public function getList()
    {
        $model = new FeedsModel();
        $res = $model->find()
            ->limit(10)
            ->with('user')
            ->orderBy(['id'=>SORT_DESC])
            ->asArray()
            ->all();
        return $res?:[];
    }

    /**
     * 留言保存
     * @return bool
     */
    public function create()
    {
        try {
            $model = new FeedsModel();
            $model->user_id = \Yii::$app->user->identity->id;
            $model->created_at = time();
            $model->content = $this->content;
            if(!$model->save())
                throw new \Exception('保存失败!');
            return true;
        }catch (\Exception $e) {
            $this->_lastError = $e->getMessage();
            return false;
        }
    }
}